package org.cobo.vmmanagement.web;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cobo.vmmanagement.domain.Project;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RequestMapping("/listprojects/**")
@Controller
public class ListProjects {

    @RequestMapping(method = RequestMethod.POST, value = "{id}")
    public void post(@PathVariable Long id, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
    }

    @RequestMapping(method = RequestMethod.GET)
    public String get(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
    	
    	List<Project> projects = Project.findAllProjects();
		// Get current user role
		String role = SecurityContextHolder.getContext().getAuthentication().getAuthorities().toString();
		boolean isManager = role.contains("ROLE_MANAGER") || role.contains("ROLE_ADMIN") ? true : false;
		
		modelMap.addAttribute("isManager", isManager);
    	modelMap.addAttribute("projects", projects);
    	
    	return "listprojects/index";
    }
    
    @RequestMapping
    public String index() {
        return "listprojects/index";
    }
    
    
}
